#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1100;
int arr[N];
int n, q;
int main()
{
	cin >> n >> q;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	while (q--) {
		int x, d;
		cin >> x >> d;
		bool f = 0;
		for (int i = 0; i < n; i++) {
			int k = arr[i];
			int ans = 0;
			bool flag = 0;
			int z = 0;
			while (k) {
				ans += (k % 10) * pow(10, z++);
				if (ans == d) {
					flag = 1;
					break;
				}
				k /= 10;
			}
			if (flag) {
				cout << arr[i] << endl;
				f = 1;
				break;
			}
		}
		if (!f) {
			cout << -1 << endl;
		}
	}
	return 0;
}